package com.yuyou.train.user.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;  import org.slf4j.LoggerFactory;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.yuyou.train.service.api.user.entity.UserHw;
import com.yuyou.train.user.service.IUserHwBeConfigService;
import com.yuyou.train.user.service.IUserHwBeService;
import com.yuyou.train.user.service.IUserHwPraiseService;
import com.yuyou.train.user.service.IUserHwService;
import com.yuyou.train.user.vo.req.ReqUserHwCorrect;
import com.yuyou.train.user.vo.resp.RespUserHw;
import com.yuyou.train.util.bean.LoginRedisObj;
import com.yuyou.train.util.bean.PageParams;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.bean.ReturnCode;
import com.yuyou.train.util.bean.ReturnResult;
import com.yuyou.train.util.enums.user.UserEnums;


@Validated
@RestController
@RequestMapping(value="/api/userHw")
public class UserHwController extends BaseController{

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IUserHwBeConfigService uerHwBeConfigService;

	@Autowired
	private IUserHwBeService userHwBeService;
	
	@Autowired
	private IUserHwPraiseService userHwPraiseService;
	
	@Autowired
	private IUserHwService userHwService;

	@ApiOperation(value = "我的作业(前台)")
	@RequestMapping(value="/findMyHwPage",method=RequestMethod.POST)
	public ReturnResult<Pagination<UserHw>> findMyHwPage(@Validated @ModelAttribute PageParams pageParams,
									 HttpServletRequest request){

		LoginRedisObj loginObj = this.getUserObj(request);
		Map<String, Object> conditions=new HashMap<>();
		Pagination<UserHw> queryPgn = new Pagination<>();
		conditions.put("userId", loginObj.getUserId());
		queryPgn.setConditions(conditions);
		queryPgn.setPageParams(pageParams);

		int total = userHwService.findUserHwPageCount(queryPgn);
		queryPgn.setTotal(total);
		queryPgn.toBePage();
		if(total > 0){
			queryPgn.setTotal(total);
			List<UserHw> list = userHwService.findUserHwPageList(queryPgn);
			queryPgn.setList(list);
		}
		return ReturnResult.success(queryPgn);
	}

	@ApiOperation(value = "老师批改作业详情(前台)")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "userHwId", value = "作业主键", paramType="query", required = true)}
			)
	@RequestMapping(value="/hwDetail",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<RespUserHw>> hwDetail(HttpServletRequest request,
			@NotBlank (message="userHwId不能为空") String userHwId){		
		LoginRedisObj obj=this.getUserObj(request);
		if(!obj.getType().equals(UserEnums.type.teacher.getValue())){
			return ReturnResult.fail(ReturnCode.code._3003.getMessage());
		}
		ResBoolSimpleInfo<RespUserHw> res=userHwService.hwDetail(userHwId,obj.getUserId());		
		return ReturnResult.success(res);
	}
	
	
	@ApiOperation(value = "老师批改作业(前台)")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "hwCorrectStr", value = "内容（{\"badList\":[\"b1\",\"b2\"],\"configList\":[{\"isOrNot\":1,\"userHwBeConfigId\":23}],\"content\":\"0e1ec994b80d4bfead1c7fb6e5a6adc6\",\"goodList\":[\"g1\",\"g2\"],\"score\":90,\"teachAttachName\":1,\"teachAttachUrl\":1,\"userHwId\":\"316945774ff148288ce95a49af930144\"}）", paramType="query", required = true)}
		)
	@RequestMapping(value="/correctHw",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> correctHw(HttpServletRequest request,
			@NotBlank (message="hwCorrectStr不能为空") String hwCorrectStr) throws Exception{			
		ReqUserHwCorrect reqUserHwCorrect=JSON.parseObject(hwCorrectStr, ReqUserHwCorrect.class);			
		LoginRedisObj obj=this.getUserObj(request);
		if(!obj.getType().equals(UserEnums.type.teacher.getValue())){
			return ReturnResult.fail(ReturnCode.code._3003.getMessage());
		}
		ResBoolSimpleInfo<String> res=userHwService.correctHw(reqUserHwCorrect,obj.getUserId());		
		return ReturnResult.success(res);
	}
	
	
	
	
	
}
